// import {waitClick, waitDisapper} from "./helper";

// waitClick('funcImage');

// waitClick();

// waitDisapper();

// waitClick();

// waitDisapper();

// waitClick();
function waitClick(name) {
    var el = desc(name).findOne();
    el.click();
}

function waitDisapper(name) {
    while (!desc(name).exists()) return;
}

function rob() {
    if (new Date().getHours() < 9) {
        setImmediate(rob);
        return;
    }

    let step = 0;

    console.log('启动成功');
    try {
        waitClick('立即抢购');
        step++;
        console.log('current', step);
        

        var order = function(cb) {
            try {
                if (desc('退出排队').exists()) {
                    step++;
                    console.log('current', step);
                    waitClick('预约抢购');
                    step++;
                    console.log('current', step);
                    cb();
                } else {
                    setTimeout(() => {
                        order(cb);
                    }, 20);
                }
            } catch (error) {
                console.log('error', error, step);
            }
        }

        var nextConfirm = () => {
            try {
                if (desc('确认').exists()) {
                    var el = desc('确认').findOne();
                    el.click();
                    waitClick('确认下单');
                    step++;
                    console.log('current', step);
                } else {
                    setTimeout(() => {
                        nextConfirm();
                    }, 20);
                }
            } catch (error) {
                console.log('error', error, step);
            }
        };

        order(nextConfirm);

    } catch (error) {
        console.error('error', error, step);
    }
}

rob();
